#!/bin/bash

# 检查 systemctl 命令是否存在
if ! command -v systemctl &> /dev/null
then
    echo "systemctl 未找到"
    exit 1
fi

# 检查 ZeroTier 服务状态
service_status=$(systemctl is-active zerotier-one)
if [ "$service_status" != "active" ]; then
    echo "ZeroTier 服务未处于活动状态"
    exit 1
fi

# 检查 zerotier-cli 是否存在
if ! command -v zerotier-cli &> /dev/null
then
    echo "zerotier-cli 未找到"
    exit 1
fi

# 检查 ZeroTier 服务是否在线
status=$(zerotier-cli info | grep "ONLINE")
if [ -z "$status" ]; then
    echo "ZeroTier 服务未在线"
    exit 1
fi

# 检查是否加入了至少一个网络
networks=$(zerotier-cli listnetworks | grep "OK")
if [ -z "$networks" ]; then
    echo "未加入任何 ZeroTier 网络"
    exit 1
fi

# 如果以上检查都通过，则表示 ZeroTier 运行正常
echo "ZeroTier 运行正常"
exit 0
